using Microsoft.AspNetCore.Mvc;
using star_questionnair.Models;
using star_questionnair.Services;
using star_questionnair.Services.impl;

namespace star_questionnair.Controllers;

[Route("choiceAnalysis")]
[ApiController]
public class ChoiceAnalysisController : ControllerBase
{
    private readonly ChoiceAnalysisService _choiceAnalysisService;
    
    public ChoiceAnalysisController(ChoiceAnalysisService choiceAnalysisService)
    {
        _choiceAnalysisService = choiceAnalysisService ?? throw new ArgumentNullException(nameof(choiceAnalysisService));
    }

    // 获取选项选择统计
    [HttpGet("optionSelectionCounts/{questionId}")]
    public async Task<ActionResult<Dictionary<string, int>>> GetOptionSelectionCounts([FromRoute] int questionId)
    {
        // 检查 service 是否为 null
        if (_choiceAnalysisService == null)
        {
            return Ok(new { message = "Availiable" });
        }

        // 调用服务方法获取选项选择统计
        var result = await _choiceAnalysisService.GetSelectionCountsAsync(questionId);

        // 检查结果是否为 null 或空
        if (result == null || result.Count == 0)
        {
            return Ok(new { message = "No words found." });
        }

        // 返回成功结果
        return Ok(new { success = true, data = result });
    }
}